package com.puboot.module.blog.controller;

import com.puboot.common.util.*;
import com.puboot.module.admin.vo.base.ResponseVo;
import com.puboot.module.blog.model.*;
import com.puboot.module.blog.service.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/blog/coal/api")
@AllArgsConstructor
public class CoalController {

    private final HttpServletRequest httpServletRequest;
    private final CoalService coalService;
    private final CoalDetailService coalDetailService;
    private final CoalEcoService coalEcoService;
    private final CoalPostMineService coalPostMineService;
    private final CoalWellWorkerService coalWellWorkerService;
    private final CoalOpenAirService coalOpenAirService;

    @PostMapping("/result")
    @Transactional
    public ResponseVo result(Coal coal) {
        try {
            HttpSession httpSession = httpServletRequest.getSession();
            Customer customer = (Customer) httpSession.getAttribute("customer");

            List<String> adWellWorkers = new ArrayList<>();
            if (StringUtils.isNotBlank(coal.getAdWellWorker())) {
                adWellWorkers = Arrays.asList(coal.getAdWellWorker().split(","));
            }
            List<String> qch4s = new ArrayList<>();
            if (StringUtils.isNotBlank(coal.getQch4())) {
                qch4s = Arrays.asList(coal.getQch4().split(","));
            }
            List<String> adOpenAirs = new ArrayList<>();
            if (StringUtils.isNotBlank(coal.getAdOpenAir())) {
                adOpenAirs = Arrays.asList(coal.getAdOpenAir().split(","));
            }
            List<String> adPostMines = new ArrayList<>();
            if (StringUtils.isNotBlank(coal.getAdPostMine())) {
                adPostMines = Arrays.asList(coal.getAdPostMine().split(","));
            }
            List<String> ncvs = new ArrayList<>();
            if (StringUtils.isNotBlank(coal.getNcv())) {
                ncvs = Arrays.asList(coal.getNcv().split(","));
            }
            List<String> fcs = new ArrayList<>();
            if (StringUtils.isNotBlank(coal.getFc())) {
                fcs = Arrays.asList(coal.getFc().split(","));
            }
            List<String> ccs = new ArrayList<>();
            if (StringUtils.isNotBlank(coal.getCc())) {
                ccs = Arrays.asList(coal.getCc().split(","));
            }
            List<String> ofs = new ArrayList<>();
            if (StringUtils.isNotBlank(coal.getOf())) {
                ofs = Arrays.asList(coal.getOf().split(","));
            }

            List<String> efPostMines = new ArrayList<>();
            if (StringUtils.isNotBlank(coal.getEfPostMine())) {
                efPostMines = Arrays.asList(coal.getEfPostMine().split(","));
            }
            List<String> adWellWorkerss = new ArrayList<>();
            if (StringUtils.isNotBlank(coal.getAdWellWorkers())) {
                adWellWorkerss = Arrays.asList(coal.getAdWellWorkers().split(","));
            }
            List<String> qco2s = new ArrayList<>();
            if (StringUtils.isNotBlank(coal.getQco2s())) {
                qco2s = Arrays.asList(coal.getQco2s().split(","));
            }
            List<String> cnns = new ArrayList<>();
            if (StringUtils.isNotBlank(coal.getCnns())) {
                cnns = Arrays.asList(coal.getCnns().split(","));
            }
            List<String> methaneNs = new ArrayList<>();
            if (StringUtils.isNotBlank(coal.getMethanens())) {
                methaneNs = Arrays.asList(coal.getMethanens().split(","));
            }

            Double eBurn = SpinUtil.getEBurn(ncvs, fcs, ccs, ofs);
            Double ech4 = CoalUtil.getEch4(adWellWorkers, qch4s, adOpenAirs, adPostMines, efPostMines, coal.getQGasTorch(),
                    coal.getMethaneCh4Torch(), coal.getQGasUtilize(), coal.getMethaneCh4Utilize());
            Double eco2 = CoalUtil.getEco2(adWellWorkerss, qco2s, cnns, methaneNs, coal.getQGasTorch()  );
            Double ePurchaseElectric = SpinUtil.getEPurchaseElectric(coal.getAdPurchaseElectric(), coal.getEfPurchaseElectric());
            Double ePurchaseHeat = SpinUtil.getEPurchaseHeatOrEOutputHeat(coal.getAdPurchaseHeat());
            Double eOutputHeat = SpinUtil.getEPurchaseHeatOrEOutputHeat(coal.getAdOutInputHeat());
            Double eOutputElectric = SpinUtil.getEOutputElectric(coal.getAdOutInputElectric(), coal.getEfOutInputElectric());
            Double result = CoalUtil.getE(eBurn, ech4, eco2, ePurchaseElectric,
                    ePurchaseHeat, eOutputElectric, eOutputHeat);
            coal.setCustomerId(customer.getId());
            coal.setCreateTime(new Date());
            coal.setEBurn(GasDCUtil.round(eBurn, 2));
            coal.setEch4(GasDCUtil.round(ech4, 2));
            coal.setEco2(GasDCUtil.round(eco2, 2));
            coal.setEPurchaseElectric(GasDCUtil.round(ePurchaseElectric, 2));
            coal.setEPurchaseHeat(GasDCUtil.round(ePurchaseHeat, 2));
            coal.setEOutputHeat(GasDCUtil.round(eOutputHeat, 2));
            coal.setEOutputElectric(GasDCUtil.round(eOutputElectric, 2));
            coal.setState(Constants.STATE_INVAILD);
            coal.setResult(GasDCUtil.getThousandth(result));
            coalService.save(coal);
            for (int i = 0; i < adWellWorkerss.size(); i++) {
                CoalEco coalEco = new CoalEco();
                coalEco.setAdWellWorker(Double.valueOf(adWellWorkers.get(i)));
                coalEco.setQco2(Double.valueOf(qco2s.get(i)));
                coalEco.setCnn(Double.valueOf(cnns.get(i)));
                coalEco.setMethaneN(Double.valueOf(methaneNs.get(i)));
                coalEco.setCoalId(coal.getId());
                coalEcoService.save(coalEco);
            }
            for (int i = 0; i < ncvs.size(); i++) {
                CoalDetail coalDetail = new CoalDetail();
                coalDetail.setNcv(Double.valueOf(ncvs.get(i)));
                coalDetail.setFc(Double.valueOf(fcs.get(i)));
                coalDetail.setCc(Double.valueOf(ccs.get(i)));
                coalDetail.setOf(Double.valueOf(ofs.get(i)));
                coalDetail.setCoalId(coal.getId());
                coalDetailService.save(coalDetail);
            }
            for (int i = 0; i < adOpenAirs.size(); i++) {
                CoalOpenAir coalOpenAir = new CoalOpenAir();
                coalOpenAir.setAdOpenAir(Double.valueOf(adOpenAirs.get(i)));
                coalOpenAir.setCoalId(coal.getId());
                coalOpenAirService.save(coalOpenAir);
            }
            for (int i = 0; i < adPostMines.size(); i++) {
                CoalPostMine coalPostMine = new CoalPostMine();
                coalPostMine.setAdPostMine(Double.valueOf(adPostMines.get(i)));
                coalPostMine.setEfPostMine(Double.valueOf(efPostMines.get(i)));
                coalPostMine.setCoalId(coal.getId());
                coalPostMineService.save(coalPostMine);
            }
            for (int i = 0; i < adWellWorkers.size(); i++) {
                CoalWellWorker coalWellWorker = new CoalWellWorker();
                coalWellWorker.setQch4(Double.valueOf(qch4s.get(i)));
                coalWellWorker.setAdWellWorker(Double.valueOf(adWellWorkers.get(i)));
                coalWellWorker.setCoalId(coal.getId());
                coalWellWorkerService.save(coalWellWorker);
            }
            return ResultUtil.success("计算成功", coal);
        } catch (Exception e) {
            log.error("计算失败", e);
            return ResultUtil.error("计算失败");
        }
    }


}
